import { http } from '../utils/http/index'

// 职位搜索请求参数
export interface JobSearchRequest {
  pageNo: number
  pageSize: number
  jobName?: string
  jobType?: string
  city?: string
  beginSalary?: number
  endSalary?: number
}

// 职位信息接口
export interface JobInfo {
  jobId: string
  jobName: string
  tags: string
  expYears: string
  edu: string
  beginSalary: number
  endSalary: number
  monthlySalary: number
  city: string
  userId: string
  name: string
  position: string
  face: string
  companyId: string
  companyName: string
  peopleSize: string
  financStage: string
}

// 职位搜索响应
export interface JobSearchResponse {
  totals: number
  totalPages: number
  list: JobInfo[]
}

/**
 * 搜索职位列表
 */
export async function searchJobs(params: JobSearchRequest): Promise<JobSearchResponse> {
  const response = await http.post('/portal/job/search/jobs', params)
  return response as JobSearchResponse
}

// 职位详情 - 职位信息
export interface JobDetailInfo {
  jobId: string
  jobName: string
  jobType: string
  expYears: string
  edu: string
  beginSalary: number
  endSalary: number
  monthlySalary: number
  jobDesc: string
  jobDescHtml: string
  tags: string
  city: string
  address: string
}

// 职位详情 - 公司信息
export interface JobDetailCompanyInfo {
  companyId: string
  companyName: string
  logo: string
  address: string
  peopleSize: string
  industry: string
  financStage: string
  benefits: string
  bonus: string
  subsidy: string
}

// 职位详情 - HR信息
export interface JobDetailHRInfo {
  hrUserId: string
  companyName: string
  name: string
  position: string
  face: string
}

// 职位详情响应
export interface JobDetailResponse {
  jobInfo: JobDetailInfo
  companyInfo: JobDetailCompanyInfo
  hrInfo: JobDetailHRInfo
}

/**
 * 获取职位详情
 */
export async function getJobDetail(jobId: string): Promise<JobDetailResponse> {
  const response = await http.get(`/portal/job/search/detail?jobId=${jobId}`)
  return response as JobDetailResponse
}

/**
 * HR获取所有职位数量（后台加载）
 */
export async function getAllJobCount(): Promise<number> {
  const response = await http.get('/portal/job/get_all_job_count', undefined, { showLoading: false })
  return response as number
}

/**
 * HR获取面试数量（后台加载）
 */
export async function getInterviewCount(): Promise<number> {
  const response = await http.get('/portal/job/interview/hr/count', undefined, { showLoading: false })
  return response as number
}

/**
 * 求职者获取收藏职位数量（后台加载）
 */
export async function getCollectedJobCount(): Promise<number> {
  const response = await http.get('/portal/job/collected/count', undefined, { showLoading: false })
  return response as number
}

/**
 * 求职者获取面试数量（后台加载）
 */
export async function getCandidateInterviewCount(): Promise<number> {
  const response = await http.get('/portal/job/interview/candidate/count', undefined, { showLoading: false })
  return response as number
}

/**
 * 检查职位是否已收藏
 */
export async function checkJobCollected(jobId: string): Promise<boolean> {
  const response = await http.get(`/portal/job/is_collected?jobId=${jobId}`, undefined, { showLoading: false })
  return response as boolean
}

/**
 * 收藏职位
 */
export async function collectJob(jobId: string): Promise<void> {
  await http.post(`/portal/job/collect?jobId=${jobId}`)
}

/**
 * 取消收藏职位
 */
export async function uncollectJob(jobId: string): Promise<void> {
  await http.post(`/portal/job/uncollect?jobId=${jobId}`)
}

// 分页参数
interface PageParams {
  pageNo: number
  pageSize: number
}

/**
 * 求职者获取收藏的职位列表
 */
export async function getCollectedJobList(params: PageParams): Promise<JobSearchResponse> {
  const response = await http.get(`/portal/job/collected/list?pageNo=${params.pageNo}&pageSize=${params.pageSize}`)
  return response as JobSearchResponse
}

// 面试进度枚举
export enum InterviewProgress {
  WAITING_ACCEPT = 1,     // 等待候选人接受面试
  ACCEPTED = 2,           // 候选人已接受面试
  REJECTED = 3,           // 候选人已拒绝面试
  CANCELLED = 4,          // HR已取消面试
  PASSED = 5,             // 面试通过
  FAILED = 6              // 面试不通过
}

// 面试信息接口
export interface InterviewInfo {
  id: string
  companyId: string
  positionId: string
  positionName: string
  candidateId: string
  hrId: string
  contactPhone: string
  remarks: string
  interviewAddress: string
  interviewAddressLng: number
  interviewAddressLat: number
  interviewProgress: InterviewProgress
  interviewTime: string
}

// 面试列表响应
export interface InterviewListResponse {
  totals: number
  totalPages: number
  list: InterviewInfo[]
}

/**
 * 求职者获取最近面试列表
 */
export async function getCandidateOngoingInterviews(params: PageParams): Promise<InterviewListResponse> {
  const response = await http.get(`/portal/job/interview/candidate/ongoing?pageNo=${params.pageNo}&pageSize=${params.pageSize}`)
  return response as InterviewListResponse
}

/**
 * 求职者获取历史面试列表
 */
export async function getCandidateHistoryInterviews(params: PageParams): Promise<InterviewListResponse> {
  const response = await http.get(`/portal/job/interview/candidate/history?pageNo=${params.pageNo}&pageSize=${params.pageSize}`)
  return response as InterviewListResponse
}

/**
 * HR获取最近面试列表
 */
export async function getHrOngoingInterviews(params: PageParams): Promise<InterviewListResponse> {
  const response = await http.get(`/portal/job/interview/hr/ongoing?pageNo=${params.pageNo}&pageSize=${params.pageSize}`)
  return response as InterviewListResponse
}

/**
 * HR获取历史面试列表
 */
export async function getHrHistoryInterviews(params: PageParams): Promise<InterviewListResponse> {
  const response = await http.get(`/portal/job/interview/hr/history?pageNo=${params.pageNo}&pageSize=${params.pageSize}`)
  return response as InterviewListResponse
}

/**
 * 获取面试详情
 */
export async function getInterviewDetail(interviewId: string): Promise<InterviewInfo> {
  const response = await http.get(`/portal/job/interview/detail?interviewId=${interviewId}`)
  return response as InterviewInfo
}

// 面试邀请请求参数
export interface InterviewInviteRequest {
  positionId: string
  positionName: string
  candidateId: string
  contactPhone: string
  remarks?: string
  interviewAddress: string
  interviewAddressLng: number
  interviewAddressLat: number
  interviewTime: string  // 格式: YYYY-MM-DD HH:mm:ss
}

/**
 * 发送面试邀请
 */
export async function inviteInterview(params: InterviewInviteRequest): Promise<string> {
  const response = await http.post('/portal/job/interview/invite', params)
  return response as string  // 返回面试ID
}

/**
 * 候选人接受面试邀请
 */
export async function acceptInterview(interviewId: string): Promise<void> {
  await http.post(`/portal/job/interview/candidate/receive?interviewId=${interviewId}`)
}

/**
 * 候选人拒绝面试邀请
 */
export async function refuseInterview(interviewId: string): Promise<void> {
  await http.post(`/portal/job/interview/candidate/refuse?interviewId=${interviewId}`)
}
